package thread_try;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class MyRunnable implements Runnable{
    @Override
    public void run() {
        for(int i=0;i<100;i++){
            System.out.println(Thread.currentThread().getName());
            try {
                Thread.sleep(0);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static void main(String[] args) {

        MyRunnable m1 = new MyRunnable();
        Thread thread1 = new Thread(m1);

        Thread thread2 = new Thread(()->{
           for (int i=0;i<10;i++){
               try {
                   Thread.sleep(0);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
               System.out.println(Thread.currentThread().getName());
           }
        });
        thread2.start();
        thread1.start();

//         其他进程结束，守护进程就会自动结束。
        Thread thread3 = new Thread(()->{
            for (int i = 0; i < 100; i++) {
                System.out.println("守护线程执行"+i);
            }
        });
        thread3.setDaemon(true);
        thread3.start();

        thread1.interrupt();// 中断线程

        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 5; i++) {
            executorService.execute(new MyRunnable());
        }
        executorService.shutdown();
    }
}
